﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>UrlGet: UrlGet.cpp Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                Examples using state machines generated by StateBuilderCpp
            </h1>
            <h3>
                UrlGet: UrlGet.cpp Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">UrlGet.cpp</div>  </div>
</div>
<div class="contents">
<a href="_url_get_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00003"></a>00003 
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;QtCore/QCoreApplication&gt;</span>
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;QtCore/QTimer&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;QtCore/QDebug&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;QtCore/QDir&gt;</span>
<a name="l00008"></a>00008 <span class="preprocessor">#include &lt;QtNetwork/QHttp&gt;</span>
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;QtNetwork/QFtp&gt;</span>
<a name="l00010"></a>00010 
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;QsLog.h&gt;</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;QsLogDest.h&gt;</span>
<a name="l00013"></a>00013 
<a name="l00014"></a>00014 <span class="preprocessor">#include &lt;SmQtObserver.h&gt;</span>
<a name="l00015"></a>00015 
<a name="l00016"></a>00016 <span class="preprocessor">#include &quot;<a class="code" href="_url_get_8h.html">UrlGet.h</a>&quot;</span>
<a name="l00017"></a>00017 <span class="preprocessor">#include &lt;UrlGetFsm.h&gt;</span> <span class="comment">// Generated header</span>
<a name="l00018"></a>00018 
<a name="l00019"></a><a class="code" href="class_url_get.html#a405d37d12a23303c45110ffe468f705b">00019</a> <a class="code" href="class_url_get.html#a405d37d12a23303c45110ffe468f705b">UrlGet::UrlGet</a>() :
<a name="l00020"></a>00020         m_pHttp(NULL),
<a name="l00021"></a>00021         m_httpHost(<span class="stringliteral">&quot;qt.nokia.com&quot;</span>),
<a name="l00022"></a>00022         m_httpFile(<span class="stringliteral">&quot;/&quot;</span>),
<a name="l00023"></a>00023         m_pFtp(NULL),
<a name="l00024"></a>00024         m_ftpHost(<span class="stringliteral">&quot;ftp.qt.nokia.com&quot;</span>),
<a name="l00025"></a>00025         m_ftpFile(<span class="stringliteral">&quot;INSTALL&quot;</span>)
<a name="l00026"></a>00026 {
<a name="l00027"></a>00027     <a class="code" href="class_url_get.html#a73259d32482632ed158a04b92ae0ab0b">logInit</a>();
<a name="l00028"></a>00028     <span class="comment">// Generated UrlGetContext</span>
<a name="l00029"></a>00029     <a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a> = <span class="keyword">new</span> UrlGetContext(*<span class="keyword">this</span>, <span class="keyword">this</span>);
<a name="l00030"></a>00030     <a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a>-&gt;SetObserver(SmQtObserver::GetInstance());
<a name="l00031"></a>00031     QObject::connect(<a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a>,
<a name="l00032"></a>00032                      SIGNAL(end()),
<a name="l00033"></a>00033                      <span class="keyword">this</span>,
<a name="l00034"></a>00034                      SLOT(<a class="code" href="class_url_get.html#ae4bbdb3d1c8126bf7f6b3d2a91b7db18">contextEnd</a>()));
<a name="l00035"></a>00035 
<a name="l00036"></a>00036 }
<a name="l00037"></a>00037 
<a name="l00038"></a><a class="code" href="class_url_get.html#a0c31673e64a115294234edb70c9f53aa">00038</a> <a class="code" href="class_url_get.html#a0c31673e64a115294234edb70c9f53aa">UrlGet::~UrlGet</a>()
<a name="l00039"></a>00039 {
<a name="l00040"></a>00040     QLOG_DEBUG();
<a name="l00041"></a>00041 }
<a name="l00042"></a>00042 
<a name="l00043"></a><a class="code" href="class_url_get.html#a73259d32482632ed158a04b92ae0ab0b">00043</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#a73259d32482632ed158a04b92ae0ab0b">UrlGet::logInit</a>()
<a name="l00044"></a>00044 {
<a name="l00045"></a>00045    QCoreApplication *pApp = QCoreApplication::instance();
<a name="l00046"></a>00046    QsLogging::Logger&amp; logger = QsLogging::Logger::instance();
<a name="l00047"></a>00047    logger.setLoggingLevel(QsLogging::TraceLevel);
<a name="l00048"></a>00048    <span class="comment">//Log to file</span>
<a name="l00049"></a>00049    <span class="keyword">const</span> QString logPath(QDir(pApp-&gt;applicationDirPath()).filePath(<span class="stringliteral">&quot;Log.txt&quot;</span>));
<a name="l00050"></a>00050    QsLogging::DestinationPtr fileDestination(
<a name="l00051"></a>00051       QsLogging::DestinationFactory::MakeFileDestination(logPath) );
<a name="l00052"></a>00052    logger.addDestination(fileDestination);
<a name="l00053"></a>00053    <span class="comment">//Log to standard output</span>
<a name="l00054"></a>00054    QsLogging::DestinationPtr debugDestination = QsLogging::DestinationFactory::MakeDebugOutputDestination();
<a name="l00055"></a>00055    logger.addDestination(debugDestination);
<a name="l00056"></a>00056 }
<a name="l00057"></a>00057 
<a name="l00058"></a><a class="code" href="class_url_get.html#ae4bbdb3d1c8126bf7f6b3d2a91b7db18">00058</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#ae4bbdb3d1c8126bf7f6b3d2a91b7db18">UrlGet::contextEnd</a>()
<a name="l00059"></a>00059 {
<a name="l00060"></a>00060     QCoreApplication::exit(0);
<a name="l00061"></a>00061 }
<a name="l00062"></a>00062 
<a name="l00063"></a><a class="code" href="class_url_get.html#ab54cae439a226ccbccce55310fd51bb7">00063</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#ab54cae439a226ccbccce55310fd51bb7">UrlGet::start</a>()
<a name="l00064"></a>00064 {
<a name="l00065"></a>00065     QLOG_INFO();
<a name="l00066"></a>00066     <a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a>-&gt;evStart();
<a name="l00067"></a>00067 }
<a name="l00068"></a>00068 
<a name="l00069"></a><a class="code" href="class_url_get.html#a9743aafc30e51855ce0c3049573f57b8">00069</a> QHttp* <a class="code" href="class_url_get.html#a9743aafc30e51855ce0c3049573f57b8">UrlGet::getHttp</a>()
<a name="l00070"></a>00070 {
<a name="l00071"></a>00071   <span class="keywordflow">if</span>(<a class="code" href="class_url_get.html#a3e788060f987572d5fba645b8b574fc2">m_pHttp</a> == NULL){
<a name="l00072"></a>00072     <a class="code" href="class_url_get.html#aac50bb8f47a15569c527c5d5d8190f54">httpInit</a>();
<a name="l00073"></a>00073   }
<a name="l00074"></a>00074   <span class="keywordflow">return</span> <a class="code" href="class_url_get.html#a3e788060f987572d5fba645b8b574fc2">m_pHttp</a>;
<a name="l00075"></a>00075 };
<a name="l00076"></a>00076 
<a name="l00077"></a><a class="code" href="class_url_get.html#aac50bb8f47a15569c527c5d5d8190f54">00077</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#aac50bb8f47a15569c527c5d5d8190f54">UrlGet::httpInit</a>()
<a name="l00078"></a>00078 {
<a name="l00079"></a>00079     <span class="keywordflow">if</span> (<a class="code" href="class_url_get.html#a3e788060f987572d5fba645b8b574fc2">m_pHttp</a> != NULL) {
<a name="l00080"></a>00080         <span class="keyword">delete</span> <a class="code" href="class_url_get.html#a3e788060f987572d5fba645b8b574fc2">m_pHttp</a>;
<a name="l00081"></a>00081     }
<a name="l00082"></a>00082     <a class="code" href="class_url_get.html#a3e788060f987572d5fba645b8b574fc2">m_pHttp</a> = <span class="keyword">new</span> QHttp(<span class="keyword">this</span>);
<a name="l00083"></a>00083     connect(<a class="code" href="class_url_get.html#a3e788060f987572d5fba645b8b574fc2">m_pHttp</a>, SIGNAL(done(<span class="keywordtype">bool</span>)), <span class="keyword">this</span>, SLOT(<a class="code" href="class_url_get.html#a512c815d3b7d5e5f964b68f6f06632b8">httpDone</a>(<span class="keywordtype">bool</span>)));
<a name="l00084"></a>00084     connect(<a class="code" href="class_url_get.html#a3e788060f987572d5fba645b8b574fc2">m_pHttp</a>, SIGNAL(stateChanged(<span class="keywordtype">int</span>)), <span class="keyword">this</span>, SLOT(<a class="code" href="class_url_get.html#aa409606fc1d39801f456da7990a8a0ef">httpStateChange</a>(<span class="keywordtype">int</span>)));
<a name="l00085"></a>00085 }
<a name="l00086"></a>00086 
<a name="l00087"></a><a class="code" href="class_url_get.html#a2d3165891395e481646a2a444bd6fcb3">00087</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#a2d3165891395e481646a2a444bd6fcb3">UrlGet::httpStart</a>()
<a name="l00088"></a>00088 {
<a name="l00089"></a>00089     QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;host: &quot;</span> &lt;&lt; <a class="code" href="class_url_get.html#a9b47cf0eab2b6a68fd12c207b97634d8">m_httpHost</a> &lt;&lt; <span class="stringliteral">&quot;, file: &quot;</span> &lt;&lt; <a class="code" href="class_url_get.html#ac3299521da7cc49b299f95a33082defd">m_httpFile</a>;
<a name="l00090"></a>00090     <a class="code" href="class_url_get.html#a9743aafc30e51855ce0c3049573f57b8">getHttp</a>()-&gt;setHost(<a class="code" href="class_url_get.html#a9b47cf0eab2b6a68fd12c207b97634d8">m_httpHost</a>);
<a name="l00091"></a>00091     <a class="code" href="class_url_get.html#a9743aafc30e51855ce0c3049573f57b8">getHttp</a>()-&gt;get(m_httpFile);
<a name="l00092"></a>00092 }
<a name="l00093"></a>00093 
<a name="l00094"></a><a class="code" href="class_url_get.html#aa409606fc1d39801f456da7990a8a0ef">00094</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#aa409606fc1d39801f456da7990a8a0ef">UrlGet::httpStateChange</a>(<span class="keywordtype">int</span> state)
<a name="l00095"></a>00095 {
<a name="l00096"></a>00096     QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;state &quot;</span> &lt;&lt; state;
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098 
<a name="l00099"></a><a class="code" href="class_url_get.html#a512c815d3b7d5e5f964b68f6f06632b8">00099</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#a512c815d3b7d5e5f964b68f6f06632b8">UrlGet::httpDone</a>(<span class="keywordtype">bool</span> error)
<a name="l00100"></a>00100 {
<a name="l00101"></a>00101     <span class="keywordflow">if</span> (error == <span class="keyword">true</span>) {
<a name="l00102"></a>00102         QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;error: &quot;</span> &lt;&lt; <a class="code" href="class_url_get.html#a9743aafc30e51855ce0c3049573f57b8">getHttp</a>()-&gt;errorString();
<a name="l00103"></a>00103         <a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a>-&gt;evHttpDoneError();
<a name="l00104"></a>00104     } <span class="keywordflow">else</span> {
<a name="l00105"></a>00105         QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;ok&quot;</span>;
<a name="l00106"></a>00106         <a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a>-&gt;evHttpDone();
<a name="l00107"></a>00107     }
<a name="l00108"></a>00108 }
<a name="l00109"></a>00109 
<a name="l00110"></a><a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">00110</a> QFtp* <a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">UrlGet::getFtp</a>()
<a name="l00111"></a>00111 {
<a name="l00112"></a>00112   <span class="keywordflow">if</span>(<a class="code" href="class_url_get.html#a13285bfd40dc20dfd75180f66c1337bf">m_pFtp</a> == NULL){
<a name="l00113"></a>00113     <a class="code" href="class_url_get.html#afd8815c27e1a6e4bef383597750f8c18">ftpInit</a>();
<a name="l00114"></a>00114   }
<a name="l00115"></a>00115   <span class="keywordflow">return</span> <a class="code" href="class_url_get.html#a13285bfd40dc20dfd75180f66c1337bf">m_pFtp</a>;
<a name="l00116"></a>00116 };
<a name="l00117"></a>00117 
<a name="l00118"></a><a class="code" href="class_url_get.html#afd8815c27e1a6e4bef383597750f8c18">00118</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#afd8815c27e1a6e4bef383597750f8c18">UrlGet::ftpInit</a>()
<a name="l00119"></a>00119 {
<a name="l00120"></a>00120     <span class="keywordflow">if</span> (<a class="code" href="class_url_get.html#a13285bfd40dc20dfd75180f66c1337bf">m_pFtp</a> != NULL) {
<a name="l00121"></a>00121         <span class="keyword">delete</span> <a class="code" href="class_url_get.html#a13285bfd40dc20dfd75180f66c1337bf">m_pFtp</a>;
<a name="l00122"></a>00122     }
<a name="l00123"></a>00123     <a class="code" href="class_url_get.html#a13285bfd40dc20dfd75180f66c1337bf">m_pFtp</a> = <span class="keyword">new</span> QFtp(<span class="keyword">this</span>);
<a name="l00124"></a>00124     connect(<a class="code" href="class_url_get.html#a13285bfd40dc20dfd75180f66c1337bf">m_pFtp</a>, SIGNAL(done(<span class="keywordtype">bool</span>)), <span class="keyword">this</span>, SLOT(<a class="code" href="class_url_get.html#a6da56590cec6c2314302c1329f9f6383">ftpDone</a>(<span class="keywordtype">bool</span>)));
<a name="l00125"></a>00125     connect(<a class="code" href="class_url_get.html#a13285bfd40dc20dfd75180f66c1337bf">m_pFtp</a>, SIGNAL(stateChanged(<span class="keywordtype">int</span>)), <span class="keyword">this</span>, SLOT(<a class="code" href="class_url_get.html#a44d0d859ba433ee67ccfcffae2e94d2b">ftpStateChange</a>(<span class="keywordtype">int</span>)));
<a name="l00126"></a>00126 }
<a name="l00127"></a>00127 
<a name="l00128"></a><a class="code" href="class_url_get.html#a6da56590cec6c2314302c1329f9f6383">00128</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#a6da56590cec6c2314302c1329f9f6383">UrlGet::ftpDone</a>(<span class="keywordtype">bool</span> error)
<a name="l00129"></a>00129 {
<a name="l00130"></a>00130     <span class="keywordflow">if</span> (error == <span class="keyword">true</span>) {
<a name="l00131"></a>00131         QLOG_INFO() &lt;&lt; error &lt;&lt; <a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">getFtp</a>()-&gt;errorString();
<a name="l00132"></a>00132         <a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a>-&gt;evFtpDoneError();
<a name="l00133"></a>00133     } <span class="keywordflow">else</span> {
<a name="l00134"></a>00134         QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;ok&quot;</span>;
<a name="l00135"></a>00135         <a class="code" href="class_url_get.html#aa5b6babde98ea04e1f46c879192b32d6">m_pUrlGetContext</a>-&gt;evFtpDone();
<a name="l00136"></a>00136     }
<a name="l00137"></a>00137 }
<a name="l00138"></a>00138 
<a name="l00139"></a><a class="code" href="class_url_get.html#a1785587199624ed070f6bb691fa460ae">00139</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#a1785587199624ed070f6bb691fa460ae">UrlGet::ftpStart</a>()
<a name="l00140"></a>00140 {
<a name="l00141"></a>00141     QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;host: &quot;</span> &lt;&lt; <a class="code" href="class_url_get.html#aba9c5a800f1b6c0b792098e7ceb700ac">m_ftpHost</a> &lt;&lt; <span class="stringliteral">&quot;, file: &quot;</span> &lt;&lt; <a class="code" href="class_url_get.html#a24ceddace31a9f2c80a61373f8b681d0">m_ftpFile</a>;
<a name="l00142"></a>00142     <a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">getFtp</a>()-&gt;connectToHost(<a class="code" href="class_url_get.html#aba9c5a800f1b6c0b792098e7ceb700ac">m_ftpHost</a>);
<a name="l00143"></a>00143     <a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">getFtp</a>()-&gt;login();
<a name="l00144"></a>00144     <a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">getFtp</a>()-&gt;cd(<span class="stringliteral">&quot;qt&quot;</span>);
<a name="l00145"></a>00145     <a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">getFtp</a>()-&gt;get(m_ftpFile);
<a name="l00146"></a>00146     <a class="code" href="class_url_get.html#afcef3056e082ff32b3fb3852eacd37f4">getFtp</a>()-&gt;close();
<a name="l00147"></a>00147 }
<a name="l00148"></a>00148 
<a name="l00149"></a><a class="code" href="class_url_get.html#a44d0d859ba433ee67ccfcffae2e94d2b">00149</a> <span class="keywordtype">void</span> <a class="code" href="class_url_get.html#a44d0d859ba433ee67ccfcffae2e94d2b">UrlGet::ftpStateChange</a>(<span class="keywordtype">int</span> state)
<a name="l00150"></a>00150 {
<a name="l00151"></a>00151     QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;state &quot;</span> &lt;&lt; state; 
<a name="l00152"></a>00152 }
<a name="l00153"></a>00153 
<a name="l00154"></a>00154 <span class="preprocessor">#include &quot;UrlGet.moc&quot;</span>
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
